const shop = require('../../../mixins/shop')
const app = getApp()
const options = {


  /**
   * 页面的初始数据
   */
  data: {
    isChoose: false,
    id: 0,
    aid: -1,
    list: [],
    addsPopup: false,
    name: '',
    mobile: '',
    address: [],
    adds: '',
    is_check: false,
    popupTitle: '新增收货地址',
    isTouchMove: false,
    startX: 0,
    startY: 0,
  },

  defaultChange(e) {
    this.setData({
      is_check: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      address: e.detail.value
    })
  },

  chooseTap(e) {
    this.setData({
      id: e.currentTarget.dataset.id
    })
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.getAdds(e.currentTarget.dataset.id);
    wx.navigateBack({});
  },

  onLoad: function (options) {
    if (options.id != 0) {
      this.setData({
        id: options.id,
        isChoose: true,
      })
    }
  },

  onShow: function () {
    this.getShopGetAddsList();
  },

  saveFun() {
    let that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!that.data.name) {
      wx.showToast({
        title: '请填写收货人',
        icon: 'none',
      })
      return
    } else if (!that.data.mobile) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
      })
      return
    } else if (that.data.mobile.length != 11 && !myreg.test(that.data.mobile)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
      })
      return
    } else if (that.data.address.length <= 0) {
      wx.showToast({
        title: '请选择收货地区',
        icon: 'none',
      })
      return
    } else if (!that.data.adds) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none',
      })
      return
    }
    that.getShopSaveAdds();
  },

  onSearchInput(e) {
    let v = e.currentTarget.dataset.var;
    let value = e.detail.value;
    this.setData({
      [v]: value
    })
  },

  updateAddress(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    let aid = e.currentTarget.dataset.aid;
    let addsinfo = that.data.list[idx];
    that.setData({
      addsPopup: true,
      aid: aid,
      name: addsinfo.name,
      mobile: addsinfo.mobile,
      address: [addsinfo.province, addsinfo.city, addsinfo.county],
      adds: addsinfo.adds,
      is_check: addsinfo.is_check,
      popupTitle: '编辑收货地址',
    })
  },

  addAddress() {
    this.setData({
      addsPopup: true,
      aid: -1,
      name: '',
      mobile: '',
      address: [],
      adds: '',
      is_check: false,
      popupTitle: '新增收货地址',
    })
  },
  deleteAdds(e) {
    let that = this;
    let idx = e.currentTarget.dataset.idx;
    let aid = e.currentTarget.dataset.aid;
    wx.showModal({
      title: '提示',
      content: '确定要删除该收货地址吗？',
      success(res) {
        if (res.confirm) {
          let list = that.data.list;
          list.splice(idx, 1);
          that.setData({
            aid: aid,
            list: list,
          })
          that.getShopDelAdds();
        }
      }
    })

  },
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.list.forEach(function (v, i) {
      if (v.isTouchMove) //只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      list: this.data.list
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index, //当前索引
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.list.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      list: that.data.list
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
}

shop(options)
Page(options)